package Grantsfire::Schema::RuntimeMixins;
use strict;
use DateTime::Format::DBI;
use URI;

=head1 Grantsfire::SchemaSugar

This module is loaded after all the Schema/Model classes have been initialized.

Right now it just adds DateTime inflation to all tables.

XXX This is a bit of a hack.  Doesn't seem possible to call this from a
component when using Schema::Loader.  Replace with the InflateColumn::DateTime
component when the current dev version of DBIx::Class becomes stable.

http://search.cpan.org/~mstrout/DBIx-Class-0.06999_02/lib/DBIx/Class/InflateColumn/DateTime.pm

=cut

package URI;
sub display_name { shift->as_string }
package Grantsfire::Schema::RuntimeMixins;

foreach my $class (map {Grantsfire::Schema->class($_)} Grantsfire::Schema->sources) {
    unless ($class->can('display_name')) {
        my @has = grep $class->has_column($_), qw[fn name title org tag identity vcard];
        no strict 'refs';
        *{"$class\::display_name"} = sub {
          my $self = shift;
          for (@has) {
            return $self->$_ if $self->$_;
          }
          return $self->id;
        }
    }
    foreach my $col ($class->columns) {
        if (defined $class->column_info($col)->{data_type} and $class->column_info($col)->{data_type} =~ /datetime/i) {
            print STDERR "Grantsfire::Schema::RuntimeMixins: Made $class.$col a Datetime\n" if Grantsfire->debug;
            $class->inflate_column($col, {
                inflate => sub { 
                    my $dbh = ref $_[1] ? $_[1]->result_source->schema->storage->dbh : $_[1]->result_source_instance->schema->storage->dbh;
                    return DateTime::Format::DBI->new($dbh)->parse_datetime($_[0]); 
                },
                deflate => sub { 
                    my $dbh = ref $_[1] ? $_[1]->result_source->schema->storage->dbh : $_[1]->result_source_instance->schema->storage->dbh;
                    return DateTime::Format::DBI->new($dbh)->format_datetime($_[0]); 
                },
            });
        }
        elsif ($col =~ /(?:\b|_)(:?ur[li]|link)(?:\b|_)/) {
            print STDERR "Grantsfire::Schema::RuntimeMixins: Made $class.$col a URI\n" if Grantsfire->debug;
            $class->inflate_column($col, {
                inflate => sub { 
                    return URI->new($_[0])->canonical; 
                },
                deflate => sub { 
                    if (ref $_[0] and ref $_[0] =~ /^URI/) {
                        return $_[0]->canonical . "";
                    }
                    else {
                        return URI->new($_[0])->canonical . ""; 
                    }
                },
            });
        }
    }
}

package Grantsfire::Schema::Feed;
use strict;

use Data::Dumper;

sub grant_total {
    my $self = shift;
    #print STDERR Dumper($self);
    $self->result_source->schema->resultset('Entry')->search(
        {
            feed => $self->id,
        }, 
        {
            select => ['SUM(amount) AS total'],
            as => [qw/total/],
        }
    )->first->get_column('total') || 0;
}

package Grantsfire::Schema::User;
use base 'Catalyst::Authentication::Store::DBIx::Class::User';

1;
